package com.atguigu.admin.controller;

import com.atguigu.admin.bean.User;
import com.atguigu.admin.exception.UserTooManyException;
import com.atguigu.admin.service.UserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Arrays;
import java.util.List;

/**
 * @author ：malichun
 * @date 2022/1/23 1:52 AM
 * @description：
 * @modified By：
 */
@Controller
public class TableController {

    @Autowired
    UserService userService;

    @GetMapping("/basic_table")
    public String basic_table(){

        // 测试异常处理
        int a= 1 / 0;


        // 放在template/table下面
        return "table/basic_table";
    }

    @GetMapping("/user/delete/{id}")
    public String deleteUser(@PathVariable("id")Long id,
                             @RequestParam(value = "pn",defaultValue = "1")Integer pn,
                             RedirectAttributes ra){

        userService.removeById(id);

        ra.addAttribute("pn",pn);
        return "redirect:/dynamic_table";
    }


    @GetMapping("/dynamic_table")
    public String dynamic_table(@RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model){
        // 表格内容的遍历
        // List<User> users = Arrays.asList(new User("zhangsan","123456"),
        //         new User("lisi","123444"),
        //         new User("haha","aaaaaa"),
        //         new User("hehe","aaaddd")
        // );
        // model.addAttribute("users",users); // 将数据放到表单遍历

        // if(users.size()>3){
        //     throw  new UserTooManyException();
        // }

        // 从数据库中查询user表中的用户进行展示
        // List<User> list = userService.list();
        // model.addAttribute("users",list);
        // userService.page()

        // 分页查询数据
        Page<User> userPage = new Page<User>(pn,2);
        // 分页查询结果
        Page<User> page = userService.page(userPage, null);

        model.addAttribute("page",page);

        return "table/dynamic_table";
    }

    @GetMapping("/responsive_table")
    public String reponsive_table(){
        return "table/responsive_table";
    }

    @GetMapping("/editable_table")
    public String editable_table(){
        return "table/editable_table";
    }
}
